//
//  TTAlipayAuthManager.m
//  PPDL
//
//  Created by 王洋 on 2019/9/9.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "TTAlipayAuthManager.h"
#import <AlipaySDK/AlipaySDK.h>

@interface TTAlipayAuthManager ()

@end

@implementation TTAlipayAuthManager

+ (TTAlipayAuthManager *)manager {
    static TTAlipayAuthManager *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        
        shareInstance = [TTAlipayAuthManager new];
    });
    return shareInstance;
}

#pragma mark 预授权&&支付    预授权就是一种支付方式
- (void)payWithOrderString:(NSString *)orderString {

    NSString *appScheme = @"com.paideli.store";
    [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        NSString * isPaySuccess = @"0";
        NSString * msg = [resultDic objectForKey:@"memo"];;
        switch ([[resultDic objectForKey:@"resultStatus"] integerValue]) {
            case 6001:
            {
                isPaySuccess = @"0";
                msg = @"用户中途取消";
            }
                break;
                
            case 6002:
            {
                isPaySuccess = @"0";
                msg = @"网络连接出错";
            }
                break;
                
            case 6004:
            {
                isPaySuccess = @"0";
                msg = @"支付结果未知（有可能已经支付成功），请查询商户订单列表中订单的支付状态";
            }
                break;
                
            case 4000:
            {
                isPaySuccess = @"0";
                msg = @"订单支付失败";
            }
                break;
                
            case 8000:
            {
                isPaySuccess = @"0";
                msg = @"正在处理中，支付结果未知（有可能已经支付成功），请查询商户订单列表中订单的支付状态";
            }
                break;
                
            case 9000:
            {
                isPaySuccess = @"1";
                msg = @"订单支付成功";
                
            }
                break;
                
            default:
                break;
        }
        
        NSMutableDictionary *payDic = @{}.mutableCopy;
        [payDic setValue:msg forKey:@"memo"];
        [payDic setValue:msg forKey:@"msg"];
        [payDic setValue:@"alipay" forKey:@"payType"];
        [payDic setValue:isPaySuccess forKey:@"resultStatus"];
        [payDic setValue:[resultDic objectForKey:@"resultStatus"] forKey:@"code"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"payResultNotification" object:nil userInfo:payDic];
    }];
}

@end
